package cn.tedu.csmall.product.webapi.controller;

import cn.tedu.csmall.commons.response.JsonResult;
import cn.tedu.csmall.pojo.dto.CategoryAddNewDTO;
import cn.tedu.csmall.pojo.vo.CategoryAddNewVO;
import cn.tedu.csmall.pojo.vo.CategoryListItemVO;
import cn.tedu.csmall.pojo.vo.CategoryStandardVO;
import cn.tedu.csmall.product.service.ICategoryService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.hibernate.validator.constraints.Mod11Check;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@Api(tags = "1.类别管理模块")
@RequestMapping("/categories")
public class CategoryController {

    @Autowired
    private ICategoryService categoryService;

    @ApiOperationSupport(order = 10)
    @PostMapping("/add-new")
    @ApiOperation("增加类别")
    public JsonResult<CategoryAddNewVO> addNew(@Valid CategoryAddNewDTO categoryAddNewDTO) {
        Long id = categoryService.addNew(categoryAddNewDTO);
        return JsonResult.ok(new CategoryAddNewVO().setId(id));
    }

    @ApiOperationSupport(order = 40)
    @ApiOperation("根据id查询类别详情")
    @ApiImplicitParam(name = "id", value = "类别id", required = true, paramType = "path", dataType = "long")
    @GetMapping("/{id:[0-9]+}")
    public JsonResult<CategoryStandardVO> getById(@PathVariable Long id) {
        return JsonResult.ok(categoryService.getById(id));
    }

    @ApiOperationSupport(order = 45)
    @ApiOperation("查询类别列表")
    @GetMapping("/list")
    public JsonResult<List<CategoryListItemVO>> list() {
        return JsonResult.ok(categoryService.list());
    }


    @ApiOperationSupport(order = 50)
    @ApiOperation("重建缓存")
    @GetMapping("/catch/rebuild")
    public JsonResult<Void> rebuildCache(){
        categoryService.rebuildCache();

        return JsonResult.ok();
    }



}
